package com.lemon.blog.web.service.impl;

import java.util.List;
import java.util.Objects;

import com.lemon.blog.web.constants.BusinessConstants;
import com.lemon.blog.web.domain.BlBlogOptions;
import com.lemon.common.core.constant.Constants;
import com.lemon.common.core.exception.ServiceException;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.blog.web.mapper.BlCommentOptionsMapper;
import com.lemon.blog.web.domain.BlCommentOptions;
import com.lemon.blog.web.service.BlCommentOptionsService;

/**
 * 包名称：com.lemon.blog.web.service.impl
 * 类名称：BlCommentOptionsServiceImpl
 * 类描述：评论相关操作Service业务层处理
 * 创建人：@author lemon
 * 创建时间：2023-08-18
 */
@Service
public class BlCommentOptionsServiceImpl extends BaseServiceImpl<BlCommentOptions> implements BlCommentOptionsService {

    @Autowired
    private BlCommentOptionsMapper blCommentOptionsMapper;

    @Override
    public List<BlCommentOptions> selectBlCommentOptionsList(BlCommentOptions blCommentOptions) {
        return blCommentOptionsMapper.selectBlCommentOptionsList(blCommentOptions);
    }

    @Override
    public int insertOrUpdate(BlCommentOptions model) {
        if (model.getCommentId() == null) {
            throw new ServiceException("错误操作！");
        }
        BlCommentOptions commentOptions = new BlCommentOptions();
        commentOptions.setUserId(SecurityUtils.getUserId());
        commentOptions.setCommentId(model.getCommentId());
        List<BlCommentOptions> existModelList = blCommentOptionsMapper.selectBlCommentOptionsList(commentOptions);
        switch (model.getBusinessType()) {
            case BusinessConstants.LIKE:
                if (existModelList.size() > 0) {
                    BlCommentOptions existModel = existModelList.get(0);
                    existModel.setDvLike(Objects.equals(existModel.getDvLike(), Constants.NUM_ZERO) ? Constants.NUM_ONE : Constants.NUM_ZERO);
                    blCommentOptionsMapper.deleteByCommentIdAndUserId(SecurityUtils.getUserId(), model.getCommentId());
                    OptEntityUtils.preUpdate(existModel);
                    return blCommentOptionsMapper.insertSelective(existModel);
                } else {
                    commentOptions.setDvLike(Constants.NUM_ONE);
                    commentOptions.setDvDislike(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(commentOptions);
                    return blCommentOptionsMapper.insertSelective(commentOptions);
                }
            case BusinessConstants.DISLIKE:
                if (existModelList.size() > 0) {
                    BlCommentOptions existModel = existModelList.get(0);
                    existModel.setDvDislike(Objects.equals(existModel.getDvDislike(), Constants.NUM_ZERO) ? Constants.NUM_ONE : Constants.NUM_ZERO);
                    blCommentOptionsMapper.deleteByCommentIdAndUserId(SecurityUtils.getUserId(), model.getCommentId());
                    OptEntityUtils.preUpdate(existModel);
                    return blCommentOptionsMapper.insertSelective(existModel);
                } else {
                    commentOptions.setDvDislike(Constants.NUM_ONE);
                    commentOptions.setDvLike(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(commentOptions);
                    return blCommentOptionsMapper.insertSelective(commentOptions);
                }
            default:
                throw new ServiceException("系统业务参数错误！");
        }
    }
}
